﻿using System;
using System.Collections.Generic;
using System.Linq;
using Complaint.Models.Dtos.Validators;

namespace Complaint.Models.Dtos
{
    public partial class Settlement : IEntity
    {
        #region Implementation of IEntity

        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (SettlementType == null)
                yield return new RuleViolation("SettlementType is required", "SettlementTypeID");
            else
            {
                if (SettlementType.SettlementTypeID == 1 && Amount == null)
                {
                    yield return new RuleViolation("Amount is required for a monetary compensation", "Amount");
                }
                if (SettlementType.SettlementTypeID == 2 && String.IsNullOrEmpty(Description))
                {
                    yield return new RuleViolation("Description is required for a replacement", "Description");
                }
            }

            if (Amount == null && String.IsNullOrEmpty(Description))
            {
                yield return new RuleViolation("Either amount or description is required");
            }

            if (String.IsNullOrEmpty(Feedback))
            {
                yield return new RuleViolation("Feedback is required", "Feedback");
            }

            if (Description != null)
            {
                if (Description.Length > 500)
                {
                    yield return new RuleViolation("Max description length is 500 characters", "Description");
                }
            }

            if (Feedback != null)
                if (Feedback.Length > 500)
                {
                    yield return new RuleViolation("Max feedback length is 500 characters", "Feedback");
                }
        }

        #endregion
    }
}
